package com.wx.tnsadmin.utils;

import com.wx.tnsadmin.model.UmsAdminModel;
import com.wx.tnsadmin.model.UmsAuthModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


public class AdminUserDetails implements UserDetails, Serializable {

    private UmsAdminModel umsAdminModel;

    private List<UmsAuthModel> umsAuthModelList;



    AdminUserDetails ( UmsAdminModel umsAdminModel,List<UmsAuthModel> umsAuthModelList) {
        this.umsAdminModel = umsAdminModel;
        this.umsAuthModelList = umsAuthModelList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities () {
        //返回当前用户的权限
        return umsAuthModelList.stream ( )
                .filter ( permission -> permission.getAuth_value ( ) != null )
                .map ( permission -> new SimpleGrantedAuthority ( permission.getAuth_value ( ) ) )
                .collect ( Collectors.toList ( ) );
    }

    @Override
    public String getUsername () {
        return umsAdminModel.getUsername ( );
    }

    @Override
    public String getPassword () {
        return umsAdminModel.getPassword ( );
    }

    @Override
    public boolean isAccountNonExpired () {
        return true;
    }

    @Override
    public boolean isAccountNonLocked () {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired () {
        return true;
    }

    @Override
    public boolean isEnabled () {
        return true;
    }
}
